package com.xuecheng.media.controller;

import com.aliyuncs.exceptions.ClientException;
import com.xuecheng.commons.model.dto.*;
import com.xuecheng.commons.model.vo.MediaVo;
import com.xuecheng.commons.model.vo.ResponseResult;
import com.xuecheng.commons.model.vo.VodUploadVo;
import com.xuecheng.commons.utils.BeanHelper;
import com.xuecheng.media.domain.Media;
import org.springframework.web.bind.annotation.*;
import com.xuecheng.media.service.MediaService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * <p>
 * 媒资信息前端控制器
 * </p>
 *
 * @author itheima
 */
@Slf4j
@RestController
@RequestMapping("/media")
public class MediaController {

    @Autowired
    private MediaService mediaService;

    @PostMapping("/vod-token")
    public ResponseResult getTokenForVideo(@RequestBody VodUploadDto dto) throws ClientException {
        return mediaService.getTokenForVideo(dto);
    }

    @GetMapping("/refresh-vod-token/{videoId}")
    public ResponseResult refreshToken(@PathVariable String videoId) throws ClientException {
        return mediaService.refreshToken(videoId);
    }

    @PostMapping
    public ResponseResult saveMedia(@RequestBody MediaDto dto) throws ClientException {
        return mediaService.saveMedia(dto);
    }

    @PostMapping("/list")
    public ResponseResult selectByPage(PageRequest pageRequest, @RequestBody MediaQueryDto dto) {


        return mediaService.selectByPage(pageRequest, dto);
    }

    @GetMapping("/preview/{mediaId}")
    public ResponseResult selectByMediaId(@PathVariable String mediaId) throws ClientException {

        return mediaService.selectByMediaId(mediaId);
    }

    @DeleteMapping("/{mediaId}")
    public ResponseResult deleteMediaById(@PathVariable String mediaId) throws ClientException {

        return mediaService.deleteMediaById(mediaId);
    }

    @PutMapping("/audit")
    public ResponseResult auditMedia(@RequestBody MediaAuditDto dto) {
        return mediaService.auditMedia(dto);
    }


    @GetMapping("/{mediaId}")
    public ResponseResult associationMediaWithTeachPlan(@PathVariable("mediaId") Long mediaId) {

        // 2.调用service查询
        Media media = mediaService.getById(mediaId);
        // 3.转vo
        MediaVo vo = BeanHelper.copyProperties(media, MediaVo.class);
        // 4.返回结果
        return ResponseResult.okResult(vo);
    }
}